<?php
class MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }
    
    	/**ADDED METHODS***/
	
	function isAllowed(){
		return TRUE;
	}
	
	function create(){
		echo "PARENT CREATE";
	}
	
	public function adminLogin($data){
		//	echo "INSIDE ADMIN LOGIN";
			$data['page'] = $this->currentPage;
			$data['action'] = $this->action;
			$data['allowed'] = false;
			
				
			$folder = 'admin';
			$this->parser->parse($folder.'/'.$this->currentPage, $data);
		
	}
	
	public function loadAdminPage($data){
			
			$data['url'] = ADMINBASEURL;
			if(!isset($this->action))
				$this->action = '';
			
			$data['action'] = $this->action;
			
			if(!isset($this->id))
				$this->id = 0;
				
			$data['id'] = $this->id;
			
			if(!isset($this->message))
				$this->message = '';
			
			$data['message'] = $this->message;	
				
			if($this->session->userdata('user_id') == null )
				return $this->adminLogin($data);
				
			if($this->currentPage == 'login')
				$this->currentPage = 'dashboard';
				
			$usertype  = $this->session->userdata('user_type');
			$data['allowed'] = false;
			
			$data['page'] = $this->currentPage;
			$data['caption'] = $this->caption;
			
			if($usertype == 'A')
				$data['allowed'] = true;

			//var_dump($data);	
			
			$folder = 'admin';
			$this->parser->parse($folder.'/includes/header', $data);
			$this->parser->parse($folder.'/'.$this->currentPage, $data);
			$this->parser->parse($folder.'/includes/footer',$data);
		
	}
	
	
	function loadClientPage($data){
			$folder = 'mcchamp';
			$page = "default";
			$data['url'] = COMPANYBASEURL;	
			if($data['page'] == ""){
				$page = "dealpage";//default
			//$data['page'] -- > $page from parameter
			}else if (!file_exists('application/views/'.$folder.'/'.$data['page'].'.php'))
			{
				//$data['page_was_found'] -- > search if page in in database
				if($data['page_was_found'])
					 $page = "default";
				
			}else{
				
				$page = "default";
				if(!$data['page_was_found'])
					 $page = $data['page'];
			}
			
		
			//$folder = 'mcchamp';
			$this->parser->parse($folder.'/'.$page, $data);
	}
	
	
	function CKEditor(){
				//Ckeditor's configuration
		$data['ckeditor'] = array (
			//ID of the textarea that will be replaced
			'id' 	=> 	'ckeditorTextarea',
			'path'	=>	'../__assets/mcchampdeals/js/ckeditor',
 
			//Optionnal values
			'config' => array (
				'toolbar' 	=> 	"Full", 	//Using the Full toolbar
				'width' 	=> 	"810px",	//Setting a custom width
				'height' 	=> 	'300px',	//Setting a custom height
 
			),
 
			//Replacing styles from the "Styles tool"
			'styles' => array (
 
				//Creating a new style named "style 1"
				'style 1' => array  (
					'name' 		=> 	'Blue Title',
					'element' 	=> 	'h2',
					'styles' => array (
						'color' 	=> 	'Blue',
						'font-weight' 	=> 	'bold'
					)
				),
 
				//Creating a new style named "style 2"
				'style 2' => array  (
					'name' 	=> 	'Red Title',
					'element' 	=> 	'h2',
					'styles' => array (
						'color' 		=> 	'Red',
						'font-weight' 		=> 	'bold',
						'text-decoration'	=> 	'underline'
					)
				)				
			)
		);
		return $data;
	}
	
	
	public function formatDate($date){
	//	echo ($date != "");
			if($date != ""){
				$dt = strtotime( $date );
				$date = date( 'y-m-d', $dt ); 
				//echo 'true';
			}else{
				echo FALSE;
			}
			return $date;
	}
	
	
	public function page($page = "" , $data = "") {
		$data['page_was_found'] = false;
		$data['categorydeallist'] = $this->mDealCategory->getAllEntries();
		//related to deals
		if($page == "" || $page == "todaysdeal"){
			$data['deals']= $this->loadDeals(TODAY,$data);
			$page = "dealpage";
		}
		
		if($page != ""){
			if($page=="hotdeals"){
				$data['deals'] = $this->loadDeals(HOT);
				$page = "dealpage";
			}
		    if($page=="pastdeals"){
				$data['deals'] = $this->loadDeals(PAST);
				$page = "dealpage";
			}
			if($page == "myaccount"){
				$data['myaccount'] = "MY ACCOUN";
				//$page="myacc"
			}
		}
		//echo $page;
		//exit;
		
		$data['page'] = $page;
		
		
		if($page != ""){
			$pageData= $this->mPage->getEntryByName($page);
			if(count($pageData) == 0) 
				$data['page_was_found'] = false;
			else{
				$data['pageData'] = $pageData[0];
				$data['page_was_found'] = true;
			}
		}

		//echo "HERE";
		$this->loadClientPage($data);
	}
	

	function loadDeals($dealtype){
		return $this->mDeal->loadDealsByType($dealtype);
	}
	
}